module Cip
  class Admin::ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    before_action :authenticate!

    helper_method :current_user, :current_user_session

    layout "cip/admin"

    private

    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end

    def authenticate!
      if !current_user_session || !current_user
        redirect_to new_cip_admin_user_session_path
      end
    end
  end
end
